#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include "ini-wrapper.h"
#include "config-info.h"

#define INI_FILENAME    "config.ini"


#define MEMBER_NUM_OF_SECTION(section_type) ((sizeof(struct section_type) - sizeof(struct section_info)) / sizeof(struct member_info))

static struct sys_config ini_config = {
    .base = {
        .ini = {.filename = INI_FILENAME, .section_num = 2},
    },
    .net = {
        .section.base = {.name = "ip", .member_num = MEMBER_NUM_OF_SECTION(network_info)},
        .ip = {.name = "ip", .type = TYPE_STRING},
        .port = {.name = "port", .type = TYPE_INT},
        .netmask = {.name = "netmask", .type = TYPE_STRING},
        .gateway = {.name = "gateway", .type = TYPE_STRING},
    },
    .log = {
        .section.base = {.name = "log", .member_num = MEMBER_NUM_OF_SECTION(log_info)},
        .log_filename = {.name = "filename", .type = TYPE_STRING},
        .level = {.name = "level", .type = TYPE_INT},
        .saveflg = {.name = "saved", .type = TYPE_BOOL},
        .filesize = {.name = "filesize", .type = TYPE_DOUBLE},
    },
};

static void config_print(void)
{
    printf("network config info:\n");
    printf("\tip:%s\n", ini_config.net.ip.v.s);
    printf("\tport:%d\n", ini_config.net.port.v.i32);
    printf("\tnetmast:%s\n", ini_config.net.netmask.v.s);
    printf("\tgateway:%s\n", ini_config.net.gateway.v.s);

    printf("log config info:\n");
    printf("\tfilename:%s\n", ini_config.log.log_filename.v.s);
    printf("\tlevel:%d\n", ini_config.log.level.v.i32);
    printf("\tsaveflg:%d\n", ini_config.log.saveflg.v.i8);
    printf("\tfilesize:%lf\n", ini_config.log.filesize.v.d64);
}


int32_t main(int argc, char *argv[])
{
    int32_t ret = 0;

    printf("member num of ip:%ld, log:%ld\n", ini_config.net.section.base.member_num, ini_config.log.section.base.member_num);

    ret = inifile_config_parse((struct ini_config *)&ini_config);
    if (0 != ret)
    {
        printf("config file parse failed\n");
        return -1;
    }
    printf("ini config file parse success\n");

    config_print();

    snprintf(ini_config.net.ip.v.s, sizeof(ini_config.net.ip.v.s), "%s", "192.168.2.33");
    ini_config.net.port.v.i32 = 1234;
    ini_config.log.level.v.i32 = 5;
    ini_config.log.filesize.v.d64 = 10.1199;

    ret = inifile_config_save((struct ini_config *)&ini_config);
    if (0 != ret)
    {
        printf("configuration save failed\n");
        return -1;
    }
    printf("ini config file save success\n");

    ret = inifile_config_parse((struct ini_config *)&ini_config);
    if (0 != ret)
    {
        printf("config file parse failed\n");
        return -1;
    }
    printf("ini config file parse success\n");
    config_print();


    snprintf(ini_config.net.ip.v.s, sizeof(ini_config.net.ip.v.s), "%s", "192.168.3.111");
    ini_config.net.port.v.i32 = 5000;
    ini_config.log.level.v.i32 = 2;
    ini_config.log.filesize.v.d64 = 50.23451;

    ret = inifile_config_save((struct ini_config *)&ini_config);
    if (0 != ret)
    {
        printf("configuration save failed\n");
        return -1;
    }
    printf("ini config file save success\n");
    return 0;
}

